package bcnet.dao;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import bcnet.domain.PriceHistoryVO;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.PreparedQuery;
import com.google.appengine.api.datastore.PropertyProjection;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.Filter;
import com.google.appengine.api.datastore.Query.FilterOperator;
import com.google.appengine.api.datastore.Query.FilterPredicate;
import com.google.appengine.api.datastore.Query.SortDirection;
import com.google.appengine.api.datastore.QueryResultIterable;
import com.google.appengine.api.datastore.QueryResultIterator;

public class PriceHistoryDao {
	public void addPriceHistory(PriceHistoryVO priceHistoryVO){
		
		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
		Entity priceHistory = new Entity("PriceHistory");
		priceHistory.setProperty("code", priceHistoryVO.getCode());
		priceHistory.setProperty("priceDate", priceHistoryVO.getPriceDate());
		priceHistory.setProperty("closePrice", priceHistoryVO.getClosePrice());
		priceHistory.setProperty("volume",priceHistoryVO.getVolume());
		priceHistory.setProperty("lowPrice",priceHistoryVO.getLowPrice());
		priceHistory.setProperty("highPrice",priceHistoryVO.getHighPrice());
	
		datastore.put(priceHistory);
		
		
	}
	
	
	public List<PriceHistoryVO> getPriceHistoryList(String code, int limit){
		
		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
		Filter filter = new FilterPredicate("code",FilterOperator.EQUAL,code);
		Query q = new Query("PriceHistory").setFilter(filter).addSort("priceDate",SortDirection.DESCENDING);
		List<Entity> results = datastore.prepare(q).asList(FetchOptions.Builder.withLimit(limit));
		List<PriceHistoryVO> list = new ArrayList<PriceHistoryVO>();
		
		for(int i=0; i<results.size(); i++){
			PriceHistoryVO priceHistoryVO = new PriceHistoryVO();
			priceHistoryVO.setCode(code);

			priceHistoryVO.setClosePrice((Double)results.get(i).getProperty("closePrice"));
			//System.out.println("HA!"+results.get(i).getProperty("closePrice"));
			priceHistoryVO.setPriceDate((Date)results.get(i).getProperty("priceDate"));
			priceHistoryVO.setVolume((Long)results.get(i).getProperty("volume"));
			priceHistoryVO.setLowPrice((Double)results.get(i).getProperty("lowPrice"));
			priceHistoryVO.setHighPrice((Double)results.get(i).getProperty("highPrice"));
			list.add(priceHistoryVO);
		}
		return list;
		
	}
	
	public List<String> getCodeList(){
		
		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
		Query q = new Query("PriceHistory");
		q.addProjection(new PropertyProjection("code", String.class));
		q.setDistinct(true);
		List<Entity> results = datastore.prepare(q).asList(FetchOptions.Builder.withDefaults());
		List<String> list = new ArrayList<String>();
		
		for(int i=0; i<results.size(); i++){
			list.add((String)results.get(i).getProperty("code"));
		}
		return list;
	}
	
	public void deleteCodeHistory(String code){
		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
		List<Key> keys = new ArrayList<Key>();
		Filter filter = new FilterPredicate("code",FilterOperator.EQUAL,code);
		Query q = new Query("PriceHistory").setFilter(filter);
		PreparedQuery pq = datastore.prepare(q);
		QueryResultIterable<Entity> result = pq.asQueryResultIterable(FetchOptions.Builder.withChunkSize(500));

		QueryResultIterator<Entity> iterator = result.iterator();
		while (iterator.hasNext()) {
		  keys.add(iterator.next().getKey());
		}

		datastore.delete(keys);
	}	
	
	
}
